/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.pojo.sqlServer.PPOutboundMission;
import me.zhengjie.pojo.sqlServer.PPOutboundMissionHistory;
import me.zhengjie.repository.PPOutboundMissionHistoryRepository;
import me.zhengjie.service.PPOutboundMissionHistoryService;
import me.zhengjie.service.dto.PPOutboundMissionHistoryDto;
import me.zhengjie.service.dto.PPOutboundMissionHistoryQueryCriteria;
import me.zhengjie.service.mapper.PPOutboundMissionHistoryMapper;
import me.zhengjie.utils.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author xinglin
* @date 2024-08-14
**/
@Service
@RequiredArgsConstructor
public class PPOutboundMissionHistoryServiceImpl implements PPOutboundMissionHistoryService {

    private final PPOutboundMissionHistoryRepository ppOutboundMissionHistoryRepository;
    private final PPOutboundMissionHistoryMapper ppOutboundMissionHistoryMapper;

    @Override
    public PageResult<PPOutboundMissionHistoryDto> queryAll(PPOutboundMissionHistoryQueryCriteria criteria, Pageable pageable){
//        String createTime=null;
//        String finishTime=null;;
//        if(criteria.getCreateTime()!=null){
//            createTime = criteria.getCreateTime();
//            int indexOfT = createTime.indexOf("T"); // 找到T的位置
//            createTime = createTime.substring(0, indexOfT); // 截取T前面的部分
//        }
//
//        if(criteria.getFinishTime()!=null){
//            finishTime = criteria.getFinishTime();
//            int indexOfT = finishTime.indexOf("T"); // 找到T的位置
//            finishTime = finishTime.substring(0, indexOfT); // 截取T前面的部分
//        }
//
//        criteria.setCreateTime(createTime);
//        criteria.setFinishTime(finishTime);
        Page<PPOutboundMissionHistory> page = ppOutboundMissionHistoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ppOutboundMissionHistoryMapper::toDto));
    }

    @Override
    public List<PPOutboundMissionHistoryDto> queryAll(PPOutboundMissionHistoryQueryCriteria criteria){
        return ppOutboundMissionHistoryMapper.toDto(ppOutboundMissionHistoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PPOutboundMissionHistoryDto findById(Integer id) {
        PPOutboundMissionHistory ppOutboundMissionHistory = ppOutboundMissionHistoryRepository.findById(id).orElseGet(PPOutboundMissionHistory::new);
        ValidationUtil.isNull(ppOutboundMissionHistory.getId(),"PpOutboundMissionHistory","id",id);
        return ppOutboundMissionHistoryMapper.toDto(ppOutboundMissionHistory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PPOutboundMissionHistory resources) {
        ppOutboundMissionHistoryRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PPOutboundMissionHistory resources) {
        PPOutboundMissionHistory ppOutboundMissionHistory = ppOutboundMissionHistoryRepository.findById(resources.getId()).orElseGet(PPOutboundMissionHistory::new);
        ValidationUtil.isNull( ppOutboundMissionHistory.getId(),"PpOutboundMissionHistory","id",resources.getId());
        ppOutboundMissionHistory.copy(resources);
        ppOutboundMissionHistoryRepository.save(ppOutboundMissionHistory);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            ppOutboundMissionHistoryRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PPOutboundMissionHistoryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PPOutboundMissionHistoryDto ppOutboundMissionHistory : all) {
            Map<String,Object> map = new LinkedHashMap<>();
//            map.put("批次号", ppOutboundMissionHistory.getBatchNum());
//            map.put("作业设备型号", ppOutboundMissionHistory.getPpDevice());
//            map.put("定位方式", ppOutboundMissionHistory.getMethod());
//            map.put("生产时间", ppOutboundMissionHistory.getPpPd());
//            map.put("过期时间", ppOutboundMissionHistory.getPpExp());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void updateStartTime(List<PPOutboundMission> ppOutBoundMissions) {
        for (PPOutboundMission ppOutboundMission : ppOutBoundMissions) {
             ppOutboundMissionHistoryRepository.updateStartTime(LocalDateTime.now(),ppOutboundMission.getMissionCode(),"任务开始");

        }
    }
}